package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.TianJieGroup;
import org.itboys.mobile.service.mysql.web.TianJieGroupService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：圣代_支付_参数_controller_web
 */
@Controller
@RequestMapping("/tianjie/web/finance/group")
public class TianJieGroupController extends BaseController{
    @Autowired
    private TianJieGroupService groupService;

    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        Model model){
            return "/tianjie/group/index";
    }


    /**
     * 分页查询
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response){
        try{
            PageResult<TianJieGroup> result=groupService.selectGroup(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }



    /**
     * 单个查询
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/findOne")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="id",required = true)Long id){
        try {
            TianJieGroup group=groupService.findOne(id);
            ResponseMessage.success(group);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/group/findOne------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);

        }
    }

    /**
     * 新增或者修改团队
     * @param request
     * @param response
     * @param group
     */
    @RequestMapping("/save")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute TianJieGroup group){
        try {
            groupService.saveGroup(group);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/group/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }

    /**
     * 新增或者修改团队
     * @param request
     * @param response
     * @param captainId
     * @param groupId
     */
    @RequestMapping("/setCaptain")
    public void setCaptain(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="captainId",required = true)Long captainId,
            @RequestParam(value="groupId",required = true)Long groupId
    ){
        try {
            groupService.setCaptain(captainId,groupId);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/group/setCaptain------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }

    /**
     * 给团队拨付任意金额
     * @param request
     * @param response
     * @param groupId
     * @param amount
     */
    @RequestMapping("/present")
    public void present(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="groupId",required = true)Long groupId,
            @RequestParam(value="amount",required = true)BigDecimal amount
    ){
        try {
            groupService.present(groupId,amount);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/group/setCaptain------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }

}
